package com.uinnova.product.eam.service.sys.impl;

import com.uinnova.product.devcloud.i18n.client.trans.LanguageGetter;
import com.uinnova.product.eam.model.EamCurrentUserInfo;
import com.uinnova.product.eam.service.sys.IEamSysSvc;
import com.uino.api.client.permission.IRoleApiSvc;
import com.uino.api.client.permission.IUserApiSvc;
import com.uino.api.client.sys.ISysApiSvc;
import com.uino.bean.permission.base.SysRole;
import com.uino.bean.permission.base.SysUser;
import com.uino.bean.permission.business.CurrentUserInfo;
import com.uino.util.sys.SysUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 系统相关服务
 *
 * @author yuhao.guan
 * @version 2020/7/28
 */
@Service
public class IEamSvcImpl implements IEamSysSvc {

    //private static Logger logger = LoggerFactory.getLogger(IEamSvcImpl.class);

    @Autowired
    private ISysApiSvc sysApiSvcSvc;

    @Value("${http.resource.space}")
    private String httpResourcePath;

    @Autowired(required = false)
    @Lazy
    private LanguageGetter languageGetter;

    @Autowired
    private IUserApiSvc userSvc;

    @Autowired
    private IRoleApiSvc roleApiSvc;


    @Override
    public String uploadFile(MultipartFile file) {
        return httpResourcePath + sysApiSvcSvc.uploadFile(file);
    }

    @Override
    public EamCurrentUserInfo getCurrentUser() {
        SysUser currentUser = SysUtil.getCurrentUserInfo();
        CurrentUserInfo user = userSvc.getCurrentUser();
        List<SysRole> roles = roleApiSvc.getRolesByUserId(user.getId());
        EamCurrentUserInfo eamCurrentUserInfo = new EamCurrentUserInfo();
        for (SysRole role:roles) {
            eamCurrentUserInfo.setId(user.getId());
            eamCurrentUserInfo.setDomainId(user.getDomainId());
            eamCurrentUserInfo.setIcon(user.getIcon());
            eamCurrentUserInfo.setLanguage(user.getLanguage());
            eamCurrentUserInfo.setUserName(user.getUserName());
            eamCurrentUserInfo.setUserCode(user.getLoginCode());
            eamCurrentUserInfo.setLastLoginTime(user.getLastLoginTime());
            eamCurrentUserInfo.setRoleName(role.getRoleName());
        }
        return eamCurrentUserInfo;
    }

}
